﻿using KinonekoSoftware.Studio.Models;
using KinonekoSoftware.Studio.Models.Shells;
using Newtonsoft.Json;

namespace KinonekoSoftware.Studio.ViewModels
{
    partial class AppViewModel
    {
        private const string Update_Url = "http://47.108.167.252/JuXiaoYou";
        
        // 
        // http://47.108.167.252/juxiaoyou?id=luoyisi&pwd=134574266&major=3&minor=6&revision=7&buildNumber=10


        internal void CheckUpdate()
        {
            Task.Run(async () =>
            {
                
                var http = new HttpClient();
                var r    = await http.GetStringAsync(Update_Url);
                var vi   = JsonConvert.DeserializeObject<VersionInformation>(r);
                var obsoleted = LaunchContext.Major       < vi.MajorVersion ||
                                LaunchContext.Minor       < vi.MinorVersion ||
                                LaunchContext.BuildNumber < vi.BuildNumber  ||
                                LaunchContext.Revision    < vi.Revision;
                
                Xplat.RunOnUIThread(() =>
                {
                    if (obsoleted)
                    {
                        Notifications.Add(new UpdateCheckNotification
                        {
                            MessageBus = _notifyQueue,
                            Urls       = vi.UpgradeUrls,
                        });
                    }
                });
                
            });
        }
    }
}